package com.yoomiito.app.ui.list

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.adapter.goods.list.NewBaoPinAdapter
import com.yoomiito.app.adapter.goods.list.NewYouPinAdapter
import com.yoomiito.app.adapter.goods.list.sort.StarStoreGoodsAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.gift.GiftGoods
import com.yoomiito.app.model.gift.GiftGoodsList
import com.yoomiito.app.model.gift.GiftType
import com.yoomiito.app.ui.order.sureorder.shopcar.ShopCarActivity
import com.yoomiito.app.ui.search.youpin.YouPinSearchActivity
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.state.ContentType
import com.yoomiito.app.widget.state.EmptyType
import com.yoomiito.app.widget.state.StatusManager
import kotlinx.android.synthetic.main.act_youpin_goods.*
import kotlinx.android.synthetic.main.fm_rcy.*
import kotlinx.android.synthetic.main.icon_shop_car.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 不带顶部主图的列表
 */
@Deprecated("区代和星店在使用,其他商品类型使用GoodsSortList_1Activity")
class GoodsSortListActivity :BaseActivity<GoodsSortListPresenter>(){

    private var mGoodsType:Int = AllCode.YouPin//初始化
    private lateinit var mAdapter:BaseQuickAdapter<GiftGoods,BaseViewHolder>
    private var mCurrentPage: Int = 1
    private var mSort: String = ""//排序
    private var mType: Long = 0 //商品类型
    private var mGoodsTypeToData:Int = 3
    override fun getLayoutId(): Int = R.layout.act_youpin_goods

    override fun newP(): GoodsSortListPresenter = GoodsSortListPresenter(App.mAppConfig)

    companion object{
        fun start(context: Context, title: String, type: Int) {
            val intent = Intent(context, GoodsSortListActivity::class.java)
            intent.putExtra(Constant.DATA, title)
            intent.putExtra(Constant.TYPE, type)
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context.startActivity(intent)
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        mGoodsType = intent.getIntExtra(Constant.TYPE, AllCode.YouPin)
        tv_center.text = intent.getStringExtra(Constant.DATA)
        iv_back_left.setOnClickListener { finish() }
        mGoodsTypeToData = when(mGoodsType){
            AllCode.BaoPin->2
            AllCode.YouPin->3
            AllCode.AgentMall->4
            AllCode.StarStore->5//星店
            else->{
                YMToast.showText("数据异常")
                finish()
                0
            }
        }
        initTabView()
        initStatusView()
        initRefresh()
        initRcy()
        showLoading()
        p.getGoodsType(mGoodsTypeToData)
        requestData()
    }

    /**
     *
     */
    private fun requestData() {
        p.getGoodsList(mSort,mCurrentPage,mType,mGoodsTypeToData)
    }

    override fun onStart() {
        super.onStart()
        if (mGoodsType == AllCode.BaoPin && App.mAppConfig.isLogin) {
            p.getList(1)
        }
    }

    private fun initTabView() {
        act_youpin_goods_tabView.setDownName("_des")
                .setUpName("_asc")
                .setPriceFirstName("sale_price")
                .setSaleFirstName("volume")
                .showSort()
                .setDefault()

//        act_youpin_goods_tabView.setOnChangeListener { sort, _ ->
//            mSort = sort
//            mCurrentPage = 1
//            requestData()
//        }

        act_youpin_goods_tabView.setOnSortClickListener { giftType ->
            mCurrentPage = 1
            mType = giftType.id
            showLoading()
            requestData()
        }

    }

    private fun initStatusView() {
        mStatusManager = StatusManager.Builder(this)
                .addType(ContentType(R.layout.fm_rcy))
                .addType(EmptyType().setEmptyContent("暂无商品"))
                .build()
        frameLayout.addView(mStatusManager.view())
    }

    private fun initRefresh() {
        fm_refreshLayout.setOnRefreshListener {
            mCurrentPage = 1
            requestData()
        }

        fm_refreshLayout.setOnLoadMoreListener {
            mCurrentPage++
            requestData()
        }
    }

    private fun initRcy() {
        fm_rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        val sd = SpacingDecoration(0, DensityUtil.dp2px(10f), false)
        fm_rcy.addItemDecoration(sd)

         when(mGoodsType){
            AllCode.YouPin-> {
                mAdapter = NewYouPinAdapter(null)//优品
            }
            AllCode.BaoPin->{
                mAdapter = NewBaoPinAdapter(null)
                val view = LayoutInflater.from(this).inflate(R.layout.icon_shop_car, null)
                view.setOnClickListener {
                    if (App.mAppConfig.isLogin) {
                        ShopCarActivity.start(this)
                    } else {
//                        AllUtils.toLogin(this, AllCode.LoginIn)
                        LoginUtils.toOneKeyLogin(this)
                    }
                }
                val lp = FrameLayout.LayoutParams(DensityUtil.dp2px(48f), DensityUtil.dp2px(48f))
                lp.bottomMargin = DensityUtil.dp2px(12f)
                lp.rightMargin = DensityUtil.dp2px(12f)
                lp.gravity = Gravity.RIGHT or Gravity.BOTTOM
                frameLayout.addView(view, lp)
            }//爆品
            AllCode.AgentMall-> mAdapter = NewYouPinAdapter(null)//区代商城
            AllCode.StarStore-> {
                fm_rcy.removeItemDecoration(sd)
                fm_rcy.addItemDecoration(SpacingDecoration(DensityUtil.dp2px(12f), DensityUtil.dp2px(10f), true))
                mAdapter =StarStoreGoodsAdapter(null)

            }//星店商城
        }
        fm_rcy.adapter =  mAdapter
        mAdapter?.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
            var info = mAdapter.data[position]
            RouterUtils.toGoodsDetail(this, info.id.toString(), mGoodsType)
        }
    }

    fun showGift(goodsList: GiftGoodsList) {
        val data = goodsList.list.data
        if (mCurrentPage == 1) {
            if (data == null || data.size == 0) {
                mStatusManager.showEmpty()
            } else {
                mStatusManager.showContent()
                mAdapter.setNewData(data)
            }
            fm_refreshLayout.finishRefresh(true)
            fm_refreshLayout.setNoMoreData(false)
        } else {
            fm_refreshLayout.finishLoadMore(true)
            if (data == null || data.size == 0) {
                Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT).show()
                fm_refreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                mAdapter.addData(data)
            }
        }
    }

    fun setType(types: MutableList<GiftType>?) {
        if (types != null) {
            types.add(0, GiftType(0, "全部", true))
            act_youpin_goods_tabView.setGiftTypes(types)
        }
    }

    fun showNewFlag(show: Boolean) {
        show_new.visibility = if (show) View.VISIBLE else View.GONE
    }
}